package avicit.bdp.dgs.qa.dto;

import avicit.bdp.common.base.BaseDTO;
import avicit.platform6.core.annotation.log.FieldRemark;
import avicit.platform6.core.annotation.log.Id;
import avicit.platform6.core.annotation.log.LogField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-03-29
 * @类说明：RuleDTO
 * @修改记录：
 * @注意事项：
 * @主要功能：质量规则DTO
 */
@ApiModel(value = "RuleDTO", description = "质量规则DTO")
@Table(name = "bdp_dgs_qa_rule")
public class RuleDTO extends BaseDTO {
    /**
     * 主键
     */
    @Id
    @LogField
    @ApiModelProperty(value = "主键")
    @FieldRemark(column = "id", field = "id", name = "id")
    @javax.persistence.Id
    @Column(name = "id")
    private String id;

    /**
     * 质量作业ID
     */
    @LogField
    @ApiModelProperty(value = "质量作业ID")
    @FieldRemark(column = "jobId", field = "jobId", name = "jobId")
    @Column(name = "job_id")
    private String jobId;

    /**
     * 名称
     */
    @LogField
    @ApiModelProperty(value = "名称")
    @FieldRemark(column = "name", field = "name", name = "名称")
    @Column(name = "name")
    private String name;

    /**
     * 描述
     */
    @LogField
    @ApiModelProperty(value = "描述")
    @FieldRemark(column = "remark", field = "remark", name = "描述")
    @Column(name = "remark")
    private String remark;

    /**
     * 强弱类型，0:强规则、1:弱规则
     */
    @LogField
    @ApiModelProperty(value = "强弱类型，0:强规则、1:弱规则")
    @FieldRemark(column = "blockingType", field = "blockingType", name = "blockingType")
    @Column(name = "blocking_type")
    private Integer blockingType;
    @Transient
    private String blockingTypeName;

    /**
     * 校验类型，0:单表校验、1:跨表比对、2:跨库比对、3:文件校验、4:单指标校验、5:多指标校验、6:根据数据标准校验
     */
    @LogField
    @ApiModelProperty(value = "校验类型，0:单表校验、1:跨表比对、2:跨库比对、3:文件校验、4:单指标校验、5:多指标校验、6:根据数据标准校验")
    @FieldRemark(column = "measureType", field = "measureType", name = "measureType")
    @Column(name = "measure_type")
    private Integer measureType;
    @Transient
    private String measureTypeName;

    /**
     * 模板类型
     */
    @LogField
    @ApiModelProperty(value = "模板类型，0:内置模板、1:自定义模板、2:自定义SQL")
    @FieldRemark(column = "templateType", field = "templateType", name = "templateType")
    @Column(name = "template_type")
    private Integer templateType;
    @Transient
    private String templateTypeName;

    /**
     * 模板ID
     */
    @LogField
    @ApiModelProperty(value = "模板ID")
    @FieldRemark(column = "templateId", field = "templateId", name = "templateId")
    @Column(name = "template_id")
    private String templateId;
    @Transient
    private String templateName;

    /**
     * 权重
     */
    @LogField
    @ApiModelProperty(value = "权重")
    @FieldRemark(column = "weight", field = "weight", name = "weight")
    @Column(name = "weight")
    private Integer weight;
    @Transient
    private Double weightRatio;

    /**
     * 异常SQL
     */
    @LogField
    @ApiModelProperty(value = "异常SQL")
    @FieldRemark(column = "abnormalSql", field = "abnormalSql", name = "abnormalSql")
    @Column(name = "abnormal_sql")
    private String abnormalSql;

    /**
     * 正常行数SQL
     */
    @LogField
    @ApiModelProperty(value = "正常行数SQL")
    @FieldRemark(column = "normalSql", field = "normalSql", name = "normalSql")
    @Column(name = "normal_sql")
    private String normalSql;

    /**
     * 脏数据处理方式，0：不保存脏数据、1：保存脏数据
     */
    @LogField
    @ApiModelProperty(value = "脏数据处理方式")
    @FieldRemark(column = "dirtyDataType", field = "dirtyDataType", name = "脏数据处理方式")
    @Column(name = "dirty_data_type")
    private Integer dirtyDataType;

    /**
     * 统计数值类型
     */
    @LogField
    @ApiModelProperty(value = "统计数值类型，0:固定值、1:字段")
    @FieldRemark(column = "statDataType", field = "statDataType", name = "统计数值类型")
    @Column(name = "stat_data_type")
    private Integer statDataType;
    @Transient
    private String statDataTypeName;

    /**
     * SQL预览
     */
    @Transient
    private String previewSql;

    /**
     * 质量规则关联的数据源列表，保存在bdp_dgs_qa_datasource表中.
     */
    @Transient
    List<QaDataSourceDTO> dataSourceList;

    /**
     * 质量规则关联的文件列表，需要从datasourceList转换而来.
     */
    @Transient
    List<FtdpFileDTO> fileList;

    /**
     * 告警配置
     */
    @Transient
    AlarmConfigDTO alarmConfig;

    /**
     * 规则参数列表
     */
    @Transient
    List<RuleTemplateArguementDTO> arguementList;

    /**
     * 规则参数JSON串
     */
    @Transient
    String variables;

    /**
     * 告警校验公式
     */
    @Transient
    private String alarmFormula;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getBlockingType() {
        return blockingType;
    }

    public void setBlockingType(Integer blockingType) {
        this.blockingType = blockingType;
    }

    public String getBlockingTypeName() {
        return blockingTypeName;
    }

    public void setBlockingTypeName(String blockingTypeName) {
        this.blockingTypeName = blockingTypeName;
    }

    public Integer getMeasureType() {
        return measureType;
    }

    public void setMeasureType(Integer measureType) {
        this.measureType = measureType;
    }

    public String getMeasureTypeName() {
        return measureTypeName;
    }

    public void setMeasureTypeName(String measureTypeName) {
        this.measureTypeName = measureTypeName;
    }

    public Integer getTemplateType() {
        return templateType;
    }

    public void setTemplateType(Integer templateType) {
        this.templateType = templateType;
    }

    public String getTemplateTypeName() {
        return templateTypeName;
    }

    public void setTemplateTypeName(String templateTypeName) {
        this.templateTypeName = templateTypeName;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Double getWeightRatio() {
        return weightRatio;
    }

    public void setWeightRatio(Double weightRatio) {
        this.weightRatio = weightRatio;
    }

    public String getAbnormalSql() {
        return abnormalSql;
    }

    public void setAbnormalSql(String abnormalSql) {
        this.abnormalSql = abnormalSql;
    }

    public String getNormalSql() {
        return normalSql;
    }

    public void setNormalSql(String normalSql) {
        this.normalSql = normalSql;
    }

    public Integer getDirtyDataType() {
        return dirtyDataType;
    }

    public void setDirtyDataType(Integer dirtyDataType) {
        this.dirtyDataType = dirtyDataType;
    }

    public String getStatDataTypeName() {
        return statDataTypeName;
    }

    public void setStatDataTypeName(String statDataTypeName) {
        this.statDataTypeName = statDataTypeName;
    }

    public String getPreviewSql() {
        return previewSql;
    }

    public void setPreviewSql(String previewSql) {
        this.previewSql = previewSql;
    }

    public List<QaDataSourceDTO> getDataSourceList() {
        return dataSourceList;
    }

    public void setDataSourceList(List<QaDataSourceDTO> dataSourceList) {
        this.dataSourceList = dataSourceList;
    }

    public List<FtdpFileDTO> getFileList() {
        return fileList;
    }

    public void setFileList(List<FtdpFileDTO> fileList) {
        this.fileList = fileList;
    }

    public AlarmConfigDTO getAlarmConfig() {
        return alarmConfig;
    }

    public void setAlarmConfig(AlarmConfigDTO alarmConfig) {
        this.alarmConfig = alarmConfig;
    }

    public List<RuleTemplateArguementDTO> getArguementList() {
        return arguementList;
    }

    public void setArguementList(List<RuleTemplateArguementDTO> arguementList) {
        this.arguementList = arguementList;
    }

    public String getVariables() {
        return variables;
    }

    public void setVariables(String variables) {
        this.variables = variables;
    }

    public Integer getStatDataType() {
        return statDataType;
    }

    public void setStatDataType(Integer statDataType) {
        this.statDataType = statDataType;
    }

    public String getAlarmFormula() {
        return alarmFormula;
    }

    public void setAlarmFormula(String alarmFormula) {
        this.alarmFormula = alarmFormula;
    }

    /**
     * 设置质量检查结果分析相关属性
     */
    public void setCalcAttribute(RuleTemplateDTO ruleTemplate) {
        AlarmConfigDTO alarm = alarmConfig;
        if (alarm != null) {
            // 设置校验公式formula
            if (StringUtils.isNotEmpty(alarm.getFormula())) {
                setAlarmFormula(alarm.getFormula());
            }
        }

        // 设置统计数值类型
        if (ruleTemplate != null) {
            setStatDataType(ruleTemplate.getStatDataType());
        }

        // 设置模板名称
        if (ruleTemplate != null) {
            setTemplateName(ruleTemplate.getName());
        }
    }

    @Override
    public String getLogFormName() {
        if (StringUtils.isEmpty(super.logFormName)) {
            return "质量规则模块";
        } else {
            return super.logFormName;
        }
    }

    @Override
    public String getLogTitle() {
        if (StringUtils.isEmpty(super.logTitle)) {
            return "质量规则";
        } else {
            return super.logTitle;
        }
    }

}
